#include<bits/stdc++.h>
#define int long long
using namespace std;
char buf[(1<<21)+5],*p1,*p2;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
	return f?-x:x;
}
int n;
int fir;
int fa[1000010];
int ans[1000010];
signed main(){
	freopen("dream.in","r",stdin);
	freopen("dream.out","w",stdout);
    n = read();
    for(int i = 1 ; i <= n ; i++){
    	int x = read(),y = read();
    	fa[x] = i;
    	if(x == 0 && y == 0) fir = i;
	}
	ans[fir] = (n - 1) * n / 2;
	int la = fir;
	while(fa[fir]){
		la = fir;
		fir = fa[fir];
		if(fir == 0) break;
		ans[fir] = ans[la] - (n / 2 - 1);
		la = fir;
		fir = fa[fir];
		ans[fir] = ans[la] - (n / 2 - 2);
	}
	for(int i = 1 ; i <= n ; i++) cout << ans[i]+ (n - 1) * n /2+(n / 2 - 4)  <<endl;
	return 0;
}

